<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('门店进货单明细和仓库出货单明细列表')" />
    <script src="https://s3.pstatp.com/cdn/expire-1-M/jquery/3.3.1/jquery.min.js"></script>
</head>
<body class="gray-bg">
     <div class="container-div">
        <div class="row">
            <div class="col-sm-12 search-collapse">
                <form id="formId">
                    <div class="select-list">
                        <ul>
                            <li>
                                <label>门店进货单号：</label>
                                <input type="text" name="pNumber" placeholder="全名"/>
                            </li>
                            <li>
                                <label>门店编号: </label>
                                <input type="text" id="sId" name="sId" placeholder="全名">
                            </li>
                            <li>
                                <label>门店名称: </label>
                                <input type="text" id="sName" name="sName" placeholder="全名">
                            </li>
                            <li>
                                <label>水果种类编号：</label>
                                <input type="text" id="fruittypesId" name="fruittypesId" placeholder="全名"/>
                            </li>
                            <li>
                                <label>水果种类名称：</label>
                                <input type="text" id="fruittypesName" name="fruittypesName" placeholder="全名"/>
                            </li>
                            <li>
                                <label>水果编号：</label>
                                <input type="text" name="fruitId" placeholder="全名"/>
                            </li>
                            <li>
                                <label>水果名称：</label>
                                <input type="text" name="fruitName"/>
                            </li>

                            <li>
                                <label>重量(kg)：</label>
                                <input type="text" name="number"/>
                            </li>
                            <li>
                                <label>单价(元/500g)：</label>
                                <input type="text" name="price"/>
                            </li>
                            <li>
                                <label>订单状态：</label>
                                <select name="isStatus" th:with="type=${@dict.getType('purchaseorderdetails')}">
                                    <option value="">所有</option>
                                    <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
                                </select>
                            </li>
                            <li>
                                <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i class="fa fa-search"></i>&nbsp;搜索</a>
                                <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i class="fa fa-refresh"></i>&nbsp;重置</a>
                            </li>
                        </ul>
                    </div>
                </form>
            </div>
            <div class="col-sm-12 select-table table-striped">
                <table id="bootstrap-table"></table>
            </div>
        </div>
    </div>
    <th:block th:include="include :: footer" />
    <script th:inline="javascript">
        var isStatusDatas = [[${@dict.getType('purchaseorderdetails')}]];
        var prefix = ctx + "inventory/purchaseorderdetails";

        $(function() {
            var options = {
                url: prefix + "/list",
                createUrl: prefix + "/add",
                updateUrl: prefix + "/edit/{id}",
                removeUrl: prefix + "/remove",
                exportUrl: prefix + "/export",
                modalName: "门店进货单明细和仓库出货单明细",
                columns: [{
                    checkbox: false
                },
                {
                    field: 'pId',
                    title: '序号',
                    formatter: function (value, row, index) {
                        return index + 1
                    }
                },
                {
                    field: 'pNumber',
                    title: '门店进货单号'
                },
                {
                    field: 'sId',
                    title: '门店编号',
                    visible: false
                },
                {
                    title: '门店名称',
                    formatter: function(value, row, index) {
                        (function () {
                            selectShopNameByShopId(row.sId, index)
                        })();
                        return ('<span id=' + "shopNameSpan" + index + '></span>')
                    }
                },
                {
                    field: 'fruittypesId',
                    title: '水果种类编号',
                    visible: false
                },
                {
                    title: '水果种类名称',
                    formatter: function(value, row, index) {
                        (function () {
                            selectFruitTypeNameByFruitTypeId(row.fruittypesId, index)
                        })();
                        return ('<span id=' + "fruitTypeNameSpan" + index + '></span>')
                    }
                },
                {
                    field: 'fruitId',
                    title: '水果编号',
                    visible: false
                },
                {
                    field: 'fruitName',
                    title: '水果名称'
                },
                {
                    field: 'number',
                    title: '重量(kg)'
                },
                {
                    field: 'price',
                    title: '单价(元/500g)'
                },
                {
                    field: 'createdBy',
                    title: '创建人'
                },
                {
                    field: 'createdTime',
                    title: '创建时间'
                },
                {
                    field: 'updatedBy',
                    title: '更新人'
                },
                {
                    field: 'updatedTime',
                    title: '更新时间'
                },
                {
                    field: 'isStatus',
                    title: '订单状态',
                    formatter: function(value, row, index) {
                       return $.table.selectDictLabel(isStatusDatas, value);
                    }
                },
                {
                    title: '操作',
                    align: 'center',
                    formatter: function(value, row, index) {
                        var actions = [];
                        var role = [[${@permission.hasRole('DragonWarehouseAdministrator')}]]; // 验证用户是否是仓库管理员
                        if(row.isStatus === 1) {
                            actions.push('<a class="btn btn-primary btn-xs ' + role +  '"  onclick="confirmAudit(' + row.pId + ',\'' + row.fruitId + '\',' + row.number + ',' + '2' + ')">通过审批</a> ');
                            actions.push('<a class="btn btn-warning btn-xs ' + role + '" onclick="confirmAudit(' + row.pId + ',\'' + row.fruitId + '\',' + row.number + ',' + '0' + ')">不通过</a> ');
                        } else if (row.isStatus === 2) {
                            console.log(row.fruitId)
                            actions.push('<a class="btn btn-danger btn-xs ' + role + '" onclick="confirmAudit(' + row.pId + ',\'' + row.fruitId + '\',' + row.number + ',' + '3' + ')">确认送达</a> ');
                        } else {
                            actions.push('<span style="font-size: 12px">无</span>')
                        }
                        return actions.join('');
                    }
                }]
            };
            $.table.init(options);
        });
    </script>

     <!--  根据水果种类名称查询水果种类编号  -->
     <script>
         $(document).ready(function () {
             $("#fruittypesName").on('input', function () {
                 var fruittypesName = $(this).val();
                 if (fruittypesName) {
                     selectFruitTypeIdByFruitTypeName(fruittypesName);
                 } else {
                     $("#fruittypesId").val(""); // 如果输入框为空就清空水果种类编号输入框
                 }
             })
         })
         function selectFruitTypeIdByFruitTypeName(fruittypesName) {
             $.ajax({
                 url: "/inventory/fruits/selectFruitTypeId",
                 dataType: "json",
                 data: {
                     "name": fruittypesName
                 },
                 type: "post",
                 success: function (resp) {
                     if (resp.fruitTypeId != null && resp.fruitTypeId !== "") {
                         $("#fruittypesId").val(resp.fruitTypeId);
                     } else {
                         $("#fruittypesId").val(""); // 如果查不到就清空水果种类编号输入框
                     }
                 },
                 error:function(xhr,type,errorThrown){
                     console.log('XMLHttpRequest 对象:',xhr);
                     console.log('错误类型:', type);
                     console.log('错误描述:', errorThrown);
                     $.modal.alertError("请求失败");
                 }
             })
         }
     </script>

     <!--  监测水果种类id输入框  -->
     <script>

         $(document).ready(function () {
             $("#fruittypesId").on('input', function () {
                 var fruittypesId = $(this).val();
                 if (fruittypesId) {
                     selectFruitTypeNameByFruitTypeId(fruittypesId);
                 } else {
                     $("#fruittypesName").val(""); // 如果输入框为空就清空水果种类编号输入框
                 }
             })
         })
         // 根据水果种类编号查询水果种类名称
         function selectFruitTypeNameByFruitTypeId(fruittypesId, index) {
             $.ajax({
                 url: "/inventory/purchaseorderdetails/selectFruitTypeName",
                 dataType: "json",
                 data: {
                     "code": fruittypesId
                 },
                 type: "post",
                 success: function (resp) {
                     if (index != null) {
                         $("#fruitTypeNameSpan" + index).html(resp.fruitTypeName);
                     } else {
                         if (resp.fruitTypeName != null && resp.fruitTypeName !== "") {
                             $("#fruittypesName").val(resp.fruitTypeName);
                         } else {
                             $("#fruittypesName").val(""); // 如果查不到就清空水果种类名称输入框
                         }
                     }
                 },
                 error:function(xhr,type,errorThrown){
                     console.log('XMLHttpRequest 对象:',xhr);
                     console.log('错误类型:', type);
                     console.log('错误描述:', errorThrown);
                     $.modal.alertError("请求失败");
                 }
             })
         }
     </script>

<!--  根据门店名称查询门店编号  -->
     <script>
         // 监测门店名称输入框
         $(document).ready(function () {
             $("#sName").on('input', function () {
                 var sName = $(this).val();
                 if (sName) {
                     selectShopIdByShopName(sName);
                 } else {
                     $("#sId").val(""); // 如果输入框为空就清空门店编号输入框
                 }
             })
         })

         function selectShopIdByShopName(sName) {
             $.ajax({
                 url: "/inventory/purchaseorderdetails/selectShopId",
                 dataType: "json",
                 data: {
                     "sName": sName
                 },
                 type: "post",
                 success: function (resp) {
                     if (resp.shopId != null && resp.shopId !== "") {
                         $("#sId").val(resp.shopId)
                     } else {
                         $("#sId").val("") // 如果查不到就清空门店编号输入框
                     }
                 },
                 error:function(xhr,type,errorThrown){
                     console.log('XMLHttpRequest 对象:',xhr);
                     console.log('错误类型:', type);
                     console.log('错误描述:', errorThrown);
                     $.modal.alertError("请求失败");
                 }
             })
         }

     </script>

<!--  根据门店编号查询门店名称  -->
     <script>
         // 监测门店编号输入框
         $(document).ready(function () {
             $("#sId").on('input', function () {
                 var sId = $(this).val();
                 if (sId) {
                     selectShopNameByShopId(sId);
                 } else {
                     $("#sName").val(""); // 如果输入框为空就清空门店名称输入框
                 }
             })
         })

         // 根据门店编号查询门店名称
         function selectShopNameByShopId(sId, index) {
             $.ajax({
                 url: "/inventory/purchaseorderdetails/selectShopName",
                 dataType: "json",
                 data: {
                     "sNumber": sId
                 },
                 type: "post",
                 success: function (resp) {
                     if (index != null) {
                         $("#shopNameSpan" + index).html(resp.shopName)
                     } else {
                         if (resp.shopName != null && resp.shopName !== "") {
                             $("#sName").val(resp.shopName)
                         } else {
                             $("#sName").val("") // 如果查不到就清空门店名称输入框
                         }
                     }
                 },
                 error:function(xhr,type,errorThrown){
                     console.log('XMLHttpRequest 对象:',xhr);
                     console.log('错误类型:', type);
                     console.log('错误描述:', errorThrown);
                     $.modal.alertError("请求失败");
                 }
             })
         }
     </script>

     <!--  审批  -->
    <script>
    function confirmAudit(pId, fruitId, number, isStatus) {
        console.log(pId, fruitId, number, isStatus);
        if (isStatus === 0) {
            $.modal.confirm('确定要驳回审批吗?', function() {
                $.modal.loading("审批正在驳回，请稍后..."); // 打开遮罩层
                setTimeout(function() {
                audit(pId, fruitId, number, isStatus);
                }, 1500); // 3000毫秒等于3秒
            });
        } else if (isStatus === 2) {
            $.modal.confirm('确定要通过审批吗?', function() {
                $.modal.loading("审批正在通过，请稍后..."); // 打开遮罩层
                setTimeout(function() {
                    audit(pId, fruitId, number, isStatus);
                }, 1500); // 3000毫秒等于3秒
            });
        } else if (isStatus === 3) {
            $.modal.confirm('您确定已经送达货物了吗?', function() {
                $.modal.loading("正在确认送达，请稍后..."); // 打开遮罩层
                setTimeout(function() {
                    audit(pId, fruitId, number, isStatus);
                }, 1500); // 3000毫秒等于3秒
            });
        }
    }
    function audit(pId, fruitId, number, isStatus) {
        $.ajax({
            url: "/inventory/purchaseorderdetails/audit",
            data: {
                "pId": pId,
                "fruitId": fruitId,
                "number": number,
                "isStatus": isStatus
            },
            dataType: "json",
            type: "post",
            success: function (resp) {
                console.log("审核返回的参数:")
                console.log(resp);
                if (resp.code === 500) {
                    $.modal.alertError(resp.msg);
                } else {
                    $.modal.msgSuccess("成功反馈");
                    $.modal.closeLoading(); // 关闭遮罩层
                    $.modal.msgSuccessReload(resp.msg);
                }
            },
            error:function(xhr,type,errorThrown){
                console.log('XMLHttpRequest 对象:',xhr);
                console.log('错误类型:', type);
                console.log('错误描述:', errorThrown);
                $.modal.alertError("请求失败");
            }
        })
    }
</script>
</body>
</html>